﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace Scala.Logic.Services
{
    public class EventValidator : IValidator<Event>
    {
        #region IValidator Members

        public bool Validate(Event instance, out string infoMessage)
        { 
            if(instance == null) 
            {
                infoMessage = "Invalid type";
                return false;
            };
         
            bool valid = true;
            var sb = new StringBuilder();
            
            IList<PropertyInfo> props = new List<PropertyInfo>(instance.GetType().GetProperties());
            
            foreach (var prop in props)
            {
                object propValue = prop.GetValue(instance, null);
                if (propValue == null && prop.Name != "Place")
                {
                    sb.Append(String.Format("Not set \"{0}\" property \n", prop.Name));
                    valid = false;
                }                          
            }

            if (instance.FromDate >= instance.TillDate)
            {
                sb.Append("From date can't be less than Till date \n");
                valid = false;
            }

            infoMessage = sb.ToString();
            return valid;
        }

        #endregion
    }
}
